﻿using System;
using log4net;
using log4net.Config;

namespace Pibby.Core.Logging
{
	public static class Log
	{
		private static readonly ILog log = LogManager.GetLogger("default");

		/// <summary>
		/// Must be called before any logging is attempted.
		/// </summary>
		public static void Configure()
		{
			// log4net requires configuration when the application starts.
			XmlConfigurator.Configure();
		}

		public static void Debug(string message)
		{
			if (log.IsDebugEnabled)
				log.Debug(message);
		}

		public static void Info(string message)
		{
			if (log.IsInfoEnabled)
				log.Info(message);
		}

		public static void Warning(string message)
		{
			if (log.IsWarnEnabled)
				log.Warn(message);
		}

		public static void Error(string message)
		{
			if (log.IsErrorEnabled)
				log.Error(message);
		}

		public static void Fatal(string message)
		{
			if (log.IsFatalEnabled)
				log.Fatal(message);
		}

		public static void UnhandledException(Exception ex)
		{
			if (ex == null)
				return;

			Error(ex.Message);

			if (ex.InnerException != null)
				Error(ex.InnerException.Message);

			if (ex.StackTrace != null)
				Error(ex.StackTrace);
		}

	}
}
